package me.guythundar.NoChat;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.logging.Logger;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class NoChat extends JavaPlugin {

	public final int HEAR_ALL = 0;
	public final int HEAR_COMMANDS_ONLY = 1;
	public final int HEAR_CHAT_ONLY = 2;
	public final int HEAR_NONE = 3;
	
	public final PlayerChatListener pcl = new PlayerChatListener(this);
	public final PlayerJoinListener pjl = new PlayerJoinListener(this);
	public final PlayerKickListener pkl = new PlayerKickListener(this);
	public final PlayerQuitListener pql = new PlayerQuitListener(this);
	public final PlayerCommandListener pcoml = new PlayerCommandListener(this);
	public final NoChatCommander cmdr = new NoChatCommander(this);
	
	
	public Dictionary<String,Integer> playerHearingLevels =
			new Hashtable<String, Integer>();
	public Logger logger = Logger.getLogger("Minecraft");
	
	public void onDisable() {
		
		//CLEANUP
		
		playerHearingLevels = new Hashtable<String, Integer>();
		// That felt dirty somehow
		
		//LOG
		logger.info( "[NoChat] DEACTIVATED." );
	}

	
	public void onEnable() {
		
		init();
		
		//EVENTS
		PluginManager pm = this.getServer().getPluginManager();
		pm.registerEvent(Event.Type.PLAYER_CHAT, pcl, Event.Priority.Highest,
				this);
		pm.registerEvent(Event.Type.PLAYER_JOIN, pjl, Event.Priority.Normal,
				this);
		pm.registerEvent(Event.Type.PLAYER_KICK, pkl, Event.Priority.Normal,
				this);
		pm.registerEvent(Event.Type.PLAYER_QUIT, pql, Event.Priority.Normal,
				this);
		
		//COMMANDS
		getCommand("hear").setExecutor(cmdr);
		
		//LOG
		logger.info( "[NoChat] ACTIVATED Version 0.1a." );
	}
	
	public void setHearingLevel( Player p, int val ){
		playerHearingLevels.remove(p.getName());
		playerHearingLevels.put(p.getName(), Integer.valueOf(val));
	}
	
	private void init() {
		Player[] ps = getServer().getOnlinePlayers();
		for ( Player p : ps ){
			setHearingLevel( p, HEAR_ALL );
		}
		
	}
}
